package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.constant.SystemConstants;
import org.summer.lawyer.domain.bo.payapp.PayAppBo;
import org.summer.lawyer.domain.vo.payapp.PayAppVo;
import org.summer.lawyer.service.IPayAppService;

import java.util.List;

/**
 * 支付应用
 *
 * @author summer
 * @date 2024-06-21
 */
@Tag(name = "后台相关接口", description = "支付应用")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/payApp")
public class PayAppController extends BaseController {

    private final IPayAppService payAppService;

    /**
     * 查询支付应用列表
     */
    @Operation(summary = "查询支付应用列表接口", description = "查询支付应用列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:app:list")
    @GetMapping("/list")
    public TableDataInfo<PayAppVo> list(PayAppBo bo, PageQuery pageQuery) {
        return payAppService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出支付应用列表
     */
    @Operation(summary = "导出支付应用列表接口", description = "导出支付应用列表")
    @SaCheckPermission("pay:app:export")
    @Log(title = "支付应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayAppBo bo, HttpServletResponse response) {
        List<PayAppVo> list = payAppService.selectPayAppVoList(bo);
        ExcelUtil.exportExcel(list, "支付应用", PayAppVo.class, response);
    }

    /**
     * 获取支付应用详细信息
     *
     * @param appId 主键
     */
    @Operation(summary = "获取支付应用详细信息接口", description = "查询支付应用列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:app:query")
    @GetMapping("/{appId}")
    public R<PayAppVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long appId) {
        return R.ok(payAppService.selectPayAppVoById(appId));
    }

    /**
     * 新增支付应用
     */
    @Operation(summary = "新增支付应用接口", description = "新增支付应用", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:app:add")
    @Log(title = "支付应用", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/savePayApp")
    public R<Void> savePayApp(@Validated(AddGroup.class) @RequestBody PayAppBo bo) {
        return toAjax(payAppService.insertByBo(bo));
    }

    /**
     * 修改支付应用
     */
    @Operation(summary = "修改支付应用接口", description = "修改支付应用", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:app:edit")
    @Log(title = "支付应用", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updatePayApp")
    public R<Void> updatePayApp(@Validated(EditGroup.class) @RequestBody PayAppBo bo) {
        return toAjax(payAppService.updateByBo(bo));
    }

    /**
     * 删除支付应用
     *
     * @param appIds 主键串
     */
    @Operation(summary = "删除支付应用接口", description = "删除支付应用", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("pay:app:remove")
    @Log(title = "删除支付应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/removePayApp/{appIds}")
    public R<Void> removePayApp(@NotEmpty(message = "主键不能为空")
                                @PathVariable Long[] appIds) {
        return toAjax(payAppService.deleteWithValidByIds(List.of(appIds), true));
    }

    @Log(title = "启用支付应用", businessType = BusinessType.UPDATE)
    @SaCheckPermission("pay:app:active")
    @PostMapping("/activePayApp")
    public R<Void> activePayApp(@RequestBody PayAppBo bo) {
        bo.setStatus(SystemConstants.ACTIVE);
        payAppService.changePayAppStatus(bo);
        return R.ok();
    }

    @Log(title = "停用支付应用", businessType = BusinessType.UPDATE)
    @SaCheckPermission("pay:app:inactive")
    @PostMapping("/inactivePayApp")
    public R<Void> inactivePayApp(@RequestBody PayAppBo bo){
        bo.setStatus(SystemConstants.INACTIVE);
        payAppService.changePayAppStatus(bo);
        return R.ok();
    }
}
